const collectService = require("../service/collect.service");
const listService = require("../service/list.service");
const dayjs = require("dayjs");

class CollectController {
  // 返回用户收藏数据
  async showCollectedList(ctx, next) {
    const userId = ctx.user.id;
    const res = await collectService.getListByUserId(userId);
    const collectedArr = new Array();
    for (const item of res) {
      const res = await listService.getListById(item.listId);
      collectedArr.push({
        ...res,
        collectionTime: dayjs(item.collectionTime).format(
          "YYYY-MM-DD HH:mm:ss"
        ),
      });
    }
    ctx.body = {
      status: 200,
      data: {
        list: collectedArr,
      },
    };
  }
  // 添加用户收藏数据
  async addCollectedList(ctx, next) {
    await collectService.addUserCollection(ctx.request.body.listId, ctx.user.id)
    ctx.body = {
      status: 201,
      message: '收藏成功'
    }
  }
  // 删除用户收藏数据
  async deletdUserCollection(ctx, next) {
    await collectService.deleteUserCollection(ctx.request.body.listId, ctx.user.id)
    ctx.body = {
      status: 201,
      message: '取消收藏成功'
    }
  }
}

module.exports = new CollectController();
